<?php 
namespace frontend\controllers;
use yii\web\Controller;

use yii\base\InvalidParamException;

use frontend\models\Resume;
use frontend\models\User;
use yii\web\UploadedFile;
use yii;
class MemberController extends Controller{
	public $layout = 'front';
	public function actionApply(){
		return $this->render('apply');
	}
	public function actionCollect(){
		return $this->render('collect');
	}
	public function actionInfo(){
		$model = new User;

		$obj = yii::$app->request;
		if($obj->isGET){
					$cookie = $obj->cookies;
					$user = unserialize($cookie->getValue('user'));
					$id = $user['user_id'];
					$res = User::find()->where('id='.$id)->one();
					if(!empty($res)){
						$model =$res;
					}
					
					return $this->render('info',['model'=>$model]);
		}else{
				$cookie = $obj->cookies;
				$user = unserialize($cookie->getValue('user'));
				$id = $user['user_id'];
			    $model->photo = UploadedFile::getInstance($model, 'photo');
			    $str = 'uploads/' .time().rand(11111,99999). '.' . $model->photo->extension;
                $model->photo->saveAs($str);
                $data = $obj->post('User');
                $data['photo']=$str;
                $res = $model->updateAll($data,['id'=>$id]);
                $model = User::find()->where('id='.$id)->one();
                if($res){
                	return $this->render('info',['model'=>$model]);
                }
		}
	}
	public function actionResume_list(){
		$cookies = Yii::$app->request->cookies;
	    $user = unserialize($cookies->getValue('user'));
	    $id = $user['user_id'];
	    $model = new Resume();
	    $res = $model->findAll(array('UserId'=>$id));


		return $this->render('resume_list',['res'=>$res]);

	}
	public function actionResume_info(){
		$id  = YII::$app->request->get('resume_id');
	
		$res = Resume::find()->where('id='.$id)->one();
		$cookie = unserialize(yii::$app->request->cookies->getValue('user'));
		$user_id = $cookie['user_id'];
		$sql = "select * from recruit_user inner join recruit_resume on recruit_user.id=recruit_resume.UserId where recruit_user.id=$user_id and recruit_resume.id=$id";
	
		$connection = \Yii::$app->db;
		$command = $connection->createCommand($sql);
	
		$res = $command->queryone();

		$ids = $res['DesiredCityName'];
		$county_sql = " select name from recruit_region where id in($ids)";

		$start = $connection->createCommand($county_sql);
	
		$city_arr = $start->queryall();
		foreach($city_arr as $v){
			$arr[]=$v['name'];
		}
		$res['DesiredCityName'] = implode(',',$arr);

		return $this->render('resume_info',['res'=>$res]);

	}
	public function actionApply_info(){
		$obj = YII::$app->request;
		if($obj->isGET){
		$model = new Resume();

		return $this->render('apply_info',['model'=>$model]);
		}
		if($obj->isPOST){
			$model = new Resume();
			$cookies = Yii::$app->request->cookies;
			$user = unserialize($cookies->getValue('user'));
			$data = $obj->post();
	
		   $arr = [];
			foreach($data['Resume'] as $k=>$v){
				if($k=='province'||$k=='city'||$k=='district'){
					$arr[]=$v;
				}
			}
			$model->DesiredCityName=implode(',', $arr);

			$model->UserId = $user['user_id'];
			if($model->load($data)&&$model->validate()){
				$model->save();
				return $this->redirect('?r=member/apply_info');
			}

		}
	
	}

     public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
            'captcha' => [
                'class' => 'yii\captcha\CaptchaAction',
                'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
            ],
            'get-region'=>[
            'class'=>\chenkby\region\RegionAction::className(),
            'model'=>\frontend\models\Region::className()
            ],
        ];
    }



}
 ?>